﻿using System;

namespace 二分搜索
{
	class Program
	{
		static void Main(string[] args)
		{
			int[] a = new int[] { 1, 3, 4, 5, 6, 8 };

			int low = 0;
			int high = a.Length - 1;
			int index = -1;
			int n = 4;//查找的数
			for(; low<=high; )
			{
				int mid = (low + high) / 2;
				if (a[mid] == n)
				{
					index = mid;
					break;
				}
				else if (a[mid] > n)
				{
					high = mid - 1;
				}
				else if (a[mid] < n)
				{
					low = mid + 1;
				}
			}
			Console.WriteLine("要查找的数在{0}的位置", index);
		}
	}
}
